#encoding=utf8
import ClearStopWord
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer

def words_prop_rate(word_list):	
	vectorizer = CountVectorizer() # 构建词汇表
	transformer = TfidfTransformer() # 该类会统计每个词语的tf-idf权值
	tfidf = transformer.fit_transform(vectorizer.fit_transform(word_list))
	#print(tfidf)
	#获取词袋模型中的所有词语
	words = vectorizer.get_feature_names()
	#print(words)
	#将tf-idf矩阵抽取出来，元素a[i][j]表示j词在i类文本中的tf-idf权重
	weight = tfidf.toarray()
	#print(weight) # 权重值0的指文本在该类文本中未出现过

	for i in range(len(weight)):
		print('-----所有词语在第%d类中的权重-----' %(i))
		for j in range(len(words)):
			print(words[j]+' : '+str(weight[i][j]))

if __name__ == '__main__':
	word_list = []
	p1 = r'../CSCMNews/体育/0.txt'
	p2 = r'../CSCMNews/娱乐/131604.txt'

	str_txt1 = ClearStopWord.read_file(p1)
	str_txt2 = ClearStopWord.read_file(p2)

	word_list1 = ' '.join(ClearStopWord.clear_for_cut(str_txt1))
	word_list2 = ' '.join(ClearStopWord.clear_for_cut(str_txt2))

	word_list.append(word_list1)
	word_list.append(word_list2)
	words_prop_rate(word_list)